package com.bby.user.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.bby.common.exception.BBYException;
import com.bby.common.model.PmMessage;
import com.bby.common.model.PmMessageDeal;
import com.bby.common.utils.BBYUtils;
import com.bby.common.utils.StatusCode;
import com.bby.user.service.DiskService;
import com.bby.user.service.MessageService;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

import static org.springframework.data.mongodb.core.query.Criteria.where;

@Service
public class MessageServiceImpl implements MessageService {

    private final MongoTemplate mongoTemplate;
    private final DiskService diskService;

    public MessageServiceImpl(MongoTemplate mongoTemplate, DiskService diskService) {
        this.mongoTemplate = mongoTemplate;
        this.diskService = diskService;
    }

    @Override
    public void addPmMessage(String userId, PmMessage message) throws BBYException {
        BBYUtils.checkEmpty(message.getTo(), message.getDetail(), message.getFileIds());
        message.setFrom(userId).setId(null).setDeal(null);
        if (message.getDate() == null) {
            message.setDate(new Date());
        }
        mongoTemplate.save(message);
    }

    @Override
    public void deletePmMessage(String userId, String messageId) throws BBYException {
        PmMessage message = mongoTemplate.findById(messageId, PmMessage.class);
        if (message == null
                || ObjectUtil.notEqual(userId, message.getFrom())
                && ObjectUtil.notEqual(userId, message.getTo())) {
            throw new BBYException(StatusCode.NOT_FOUND,"没有这个信息！");
        }
        mongoTemplate.remove(new Query(where("_id").is(messageId)),PmMessage.class);
    }

    @Override
    public List<PmMessage> listPmMessage(String userId, String action, boolean all) throws BBYException {
        if (!action.matches("receive|send")) {
            throw new BBYException(StatusCode.INPUT_ERROR, "参数错误！");
        }
        Criteria query =
                where("send".equals(action) ? "from" : "to")
                        .is(userId);
        if (!all) {
            query.and("deal").isNull();
        }
        return mongoTemplate.find(new Query(query), PmMessage.class);
    }

    @Override
    public void dealPmMessage(String userId, PmMessageDeal deal) throws BBYException {
        BBYUtils.checkEmpty(deal.getMessageId(), deal.getType());
        PmMessage message = mongoTemplate.findById(deal.getMessageId(), PmMessage.class);
        if (message == null) {
            throw new BBYException(StatusCode.NOT_FOUND, "请求信息被作者吃了");
        }
        if (ObjectUtil.notEqual(message.getTo(), userId)) {
            throw new BBYException(StatusCode.NON_AUTHORITY, "请勿非法使用！");
        }
        if (!"reject".equals(deal.getType())){
//        Boolean res = diskService.dealPmMessage(deal);
        }
        message.setDeal(deal);
        mongoTemplate.updateFirst(
                new Query(where("_id").is(message.getId())),
                new Update().set("deal", deal),
                PmMessage.class);
    }
}
